package com.wingsfrog.puzzle.util;

import com.qiniu.util.Auth;
import java.io.IOException;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class QiniuUtil {
    private static Logger logger = Logger.getLogger(QiniuUtil.class);
    private final static String ACCESS_KEY = "_v06qM_cbDQ2wlP6YsTEBbF9vFbXPTbniGMkadpi";
    private final static String SECRET_KEY = "lQpzPoLg4TAgczUOlJ0hKmrOuHbNOHY535Qb1F4M";
    private final static String bucketName = "georgewang";
    private final static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    private final static UploadManager uploadManager = new UploadManager();

    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    private static String getUpToken(){
        return auth.uploadToken(bucketName);
    }

    public static void upload(File file, String filename) throws IOException{
        try {
            //调用put方法上传
            Response res = uploadManager.put(file, filename, getUpToken());
            //打印返回的信息
            System.out.println(res.bodyString());
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
        }
    }
}
